package com.fss.order.impl;

import com.fss.order.domain.Order;
import com.fss.order.domain.OrderSku;
import com.fss.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService
{
    @Autowired
    private OrderMapper orderMapper;

       // 创建订单（事务管理）
    @Transactional
    public void createOrder(Order order) {
        // 插入订单主表
        orderMapper.insertOrder(order);

        // 插入订单详情表
        List<OrderSku> orderSkus = order.getOrderSkus();
        if (orderSkus != null && !orderSkus.isEmpty()) {
            orderMapper.insertOrderSku(order.getOrderId(),orderSkus);
        }
    }

    // 根据 userId 查询订单
    public List<Order> getOrderByUserId(Long userId) {
        return orderMapper.getOrderByUserId(userId);
    }
     @Override
    public int deleteOrderById(Long orderId) {
        // 删除订单详情（SKU 信息）
        int deletedOrderSku = orderMapper.deleteOrderSkuByOrderId(orderId);

        // 删除订单
        int deletedOrder = orderMapper.deleteOrderById(orderId);

        // 如果两个删除操作都成功，则返回 true
        return deletedOrderSku > 0 && deletedOrder > 0?1:0;
    }

}
